package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import org.come.Frame.ZhuFrame;
import org.come.entity.TeamRole;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.AnalysisString;
import org.come.until.FormsManagement;
import org.come.until.Util;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.TeamPanelBtn;
import com.tool.image.ImageMixDeal;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;

 

public class TeamApplyJpanel extends JPanel {

    private JTable jTable;// 队伍的人员信息表格
    private DefaultTableModel defaultTableModel;// 队伍的人员信息表格对象

    private Vector<Vector<String>> verVectors;// 设置所有帮派成员的集合

    private TeamPanelBtn btnAgree,// 允许
            btnRefuse,// 拒绝
            btnClear;// 清空

    public TeamApplyJpanel() throws Exception {
		if (MyIsif.getStyle().equals("水墨UI")) {
    		 this.setPreferredSize(new Dimension(344, 284));
    	        this.setLayout(null);
    	        this.setBackground(UIUtils.Color_BACK);

    	        // 关闭按钮事件
    	        FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 30);
    	        offBtn.setBounds(344 - 29, 4, 25, 25);
    	        this.add(offBtn);

    	        // 队伍的人员信息表格
    	        jTable = new JTable() {{setOpaque(false);}
    	            @Override
    	            public boolean isCellEditable(int row, int column) {
    	                return false;
    	            }// 表格不允许被编辑
    	        };
    	        // 队伍的人员信息表格对象
    	        defaultTableModel = new DefaultTableModel();
    	        verVectors = new Vector<Vector<String>>();
    	        defaultTableModel.setDataVector(verVectors, Util.vector2);
    	        // 队伍的人员信息表格
    	        jTable.setShowGrid(false);// 设置表格的边框线条不显示

    	        // 设置表格内部信息居中显示
    	        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    	        renderer.setHorizontalAlignment(SwingConstants.CENTER);
    	        jTable.setDefaultRenderer(Object.class, renderer);
    	        jTable.setSelectionBackground(new Color(8, 8, 0));
    	        jTable.setSelectionForeground(Color.red);
    	        jTable.setForeground(Color.red);
    	        jTable.setFont(new Font("微软雅黑", 0, 12));
    	        jTable.setBackground(UIUtils.Color_BACK);
    	        jTable.setModel(defaultTableModel);

    	        // 设置每一列的宽度
    	        jTable.getColumnModel().getColumn(0).setPreferredWidth(90);
    	        jTable.getColumnModel().getColumn(1).setPreferredWidth(120);
    	        jTable.getColumnModel().getColumn(2).setPreferredWidth(60);
    	        jTable.getColumnModel().getColumn(3).setPreferredWidth(80);

    	        jTable.setBounds(25, 68, 292, 170);
    	        this.add(jTable);

    	        // 允许
    	        btnAgree = new TeamPanelBtn("skin/8100226/newbutton/B20.png", 1, "允许", UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, null,
    	                this);
    	        btnAgree.setBounds(50, 240, 59, 24);
    	        this.add(btnAgree);

    	        // 拒绝
    	        btnRefuse = new TeamPanelBtn("skin/8100226/newbutton/B20.png", 1, "拒绝", UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, null,
    	                this);
    	        btnRefuse.setBounds(150, 240, 59, 24);
    	        this.add(btnRefuse);

    	        // 清空
    	        btnClear = new TeamPanelBtn("skin/8100226/newbutton/B20.png", 1, "清空", UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, null,
    	                this);
    	        btnClear.setBounds(250, 240, 59, 24);
    	        this.add(btnClear);
    }else {
			this.setPreferredSize(new Dimension(333, 277));
			this.setLayout(null);
			this.setBackground(UIUtils.Color_BACK);

			// 关闭按钮事件
			FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/hongmu/s74.png", 1, 30);
			offBtn.setBounds(308, 0, 25, 25);
			this.add(offBtn);

			// 队伍的人员信息表格
			jTable = new JTable() {
				{
					setOpaque(false);
				}

				@Override
				public boolean isCellEditable(int row, int column) {
					return false;
				}// 表格不允许被编辑
			};
			// 队伍的人员信息表格对象
			defaultTableModel = new DefaultTableModel();
			verVectors = new Vector<Vector<String>>();
			defaultTableModel.setDataVector(verVectors, Util.vector2);
			// 队伍的人员信息表格
			jTable.setShowGrid(false);// 设置表格的边框线条不显示

			// 设置表格内部信息居中显示
			DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
			renderer.setHorizontalAlignment(SwingConstants.CENTER);
			jTable.setDefaultRenderer(Object.class, renderer);
			jTable.setSelectionBackground(new Color(8, 8, 0));
			jTable.setSelectionForeground(Color.red);
			jTable.setForeground(Color.red);
			jTable.setFont(new Font("微软雅黑", 0, 12));
			jTable.setBackground(UIUtils.Color_BACK);
			jTable.setModel(defaultTableModel);

			// 设置每一列的宽度
			jTable.getColumnModel().getColumn(0).setPreferredWidth(90);
			jTable.getColumnModel().getColumn(1).setPreferredWidth(100);
			jTable.getColumnModel().getColumn(2).setPreferredWidth(60);
			jTable.getColumnModel().getColumn(3).setPreferredWidth(80);

			jTable.setBounds(21, 65, 292, 170);
			this.add(jTable);

			// 允许
			btnAgree = new TeamPanelBtn("inkImg/hongmu/1/6026.png", 1, "允许", UIUtils.COLOR_YELLOW1, UIUtils.TEXT_HY88, null,
					this);
			btnAgree.setBounds(40, 225, 60, 26);
			this.add(btnAgree);

			// 拒绝
			btnRefuse = new TeamPanelBtn("inkImg/hongmu/1/6026.png", 1, "拒绝", UIUtils.COLOR_YELLOW1, UIUtils.TEXT_HY88, null,
					this);
			btnRefuse.setBounds(140, 225, 60, 26);
			this.add(btnRefuse);

			// 清空
			btnClear = new TeamPanelBtn("inkImg/hongmu/1/6026.png", 1, "清空", UIUtils.COLOR_YELLOW1, UIUtils.TEXT_HY88, null,
					this);
			btnClear.setBounds(240, 225, 60, 26);
			this.add(btnClear);
		}
	}
    ImageIcon icon;
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 组队
//        if (icon == null)
		if (MyIsif.getStyle().equals("水墨UI")) {
			icon = new ImageIcon("inkImg/newbackground/B229.png");
			g.drawImage(icon.getImage(), 0, 0, 344, 284, this);
		}else {
			icon = new ImageIcon("inkImg/hongmu/2/102_png.xy2uiimg.team_request_panel.png");
			g.drawImage(icon.getImage(), 0, 0, 333, 277, this);
		}
    }
    /**允许*/
    public void teamAgree(){
    	int index=jTable.getSelectedRow();
    	if (index==-1) {
    		ZhuFrame.getZhuJpanel().addPrompt2("请选择你要拒绝加入的对象！");
    		return;
		}
    	if (ImageMixDeal.userimg.getRoleShow().getTroop_id()==null) {
    		ZhuFrame.getZhuJpanel().addPrompt2("你已经没有队伍了");
    		return;
		}
    	if (ImageMixDeal.userimg.getTeams()==null||ImageMixDeal.userimg.getTeams().length>=5) {
    		ZhuFrame.getZhuJpanel().addPrompt2("无法加人");
    		return;
		}
    	TeamRole teamRole=teamRoles.remove(index);
    	verVectors.remove(index);
    	String sendmes = Agreement.getAgreement().team5Agreement("A"+teamRole.getRoleId());
		SendMessageUntil.toServer(sendmes);
    }
    /**拒绝*/
    public void teamRefruse(){
    	int index=jTable.getSelectedRow();
    	if (index==-1) {
    		ZhuFrame.getZhuJpanel().addPrompt2("请选择你要拒绝加入的对象！");
    		return;
		}
    	TeamRole teamRole=teamRoles.remove(index);
    	verVectors.remove(index);
    	String sendmes = Agreement.getAgreement().team5Agreement("R"+teamRole.getRoleId());
		SendMessageUntil.toServer(sendmes);
	}
    /**清空*/
    public void teamClear(){
    	this.teamRoles.clear();
    	verVectors.removeAllElements();
		String sendmes = Agreement.getAgreement().team5Agreement("E");
		SendMessageUntil.toServer(sendmes);
    }
    List<TeamRole> teamRoles=new ArrayList<>();
    /**展示申请列表数据*/
    public void showTeamRoleList(List<TeamRole> teamRoles) {
        // 先将组队申请面板中的信息清除
    	this.teamRoles.clear();
    	this.verVectors.removeAllElements();
    	for (int j = 0; j < teamRoles.size(); j++) {
        	TeamRole teamRole=teamRoles.get(j);
            showTeamRole(teamRole);
        }
        FormsManagement.showForm(30); 
    }
    /**展示申请列表数据*/
    public void showTeamRole(TeamRole teamRole) {
		// 将申请加入队伍的人的信息加入申请申请的面板中
		Vector<String> vector = new Vector<String>();
		vector.add(0, teamRole.getName());
		vector.add(1, AnalysisString.lvl(teamRole.getGrade()) + "");
		vector.add(2, Util.getRaceSting(teamRole.getSpeciesId()));
		vector.add(3, Util.getSexSting(teamRole.getSpeciesId()));
		teamRoles.add(teamRole);
	    verVectors.add(vector);
	}
}
